package com.iw.adoms.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.iw.adoms.entity.Department;
import com.iw.adoms.entity.MsgNotice;
import com.iw.adoms.entity.MsgNoticeType;
import com.iw.adoms.service.DepartmentService;
import com.iw.adoms.service.MsgNoticeService;
import com.iw.adoms.service.MsgNoticeTypeService;
import com.iw.core.orm.PageData;
import com.iw.core.security.entity.Users;
import com.iw.core.utils.WebUtils;

@Controller
@RequestMapping("/adoms/msgNotice")
public class MsgNoticeAction {
	@Resource
	private MsgNoticeService msgNoticeService;
	@Resource
	private MsgNoticeTypeService msgNoticeTypeService;
	@Resource
	private DepartmentService departmentService;

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request) {
		PageData<MsgNotice> pageData = new PageData<MsgNotice>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = msgNoticeService.find(pageData);

		return new ModelMap(pageData);
	}

	@RequestMapping("/myNotice")
	public ModelMap myNotice(HttpServletRequest request) {
		PageData<MsgNotice> pageData = new PageData<MsgNotice>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = msgNoticeService.find(pageData);

		return new ModelMap(pageData);
	}

	@RequestMapping("/view/{id}")
	public String view(@PathVariable("id") Integer id, Model model) {
		MsgNotice notice = msgNoticeService.find(id);
		model.addAttribute("notice", notice);
		return "/adoms/msgNotice/view";
	}

	@RequestMapping(value = "/new")
	public String addForm(Model model, HttpServletRequest request) {
		commonModel(model, request);
		return "/adoms/msgNotice/input";
	}

	private void commonModel(Model model, HttpServletRequest request) {
		List<MsgNoticeType> typeList = msgNoticeTypeService.findAll();
		model.addAttribute("typeList", typeList);
		Integer dId = getCurrentUser(request).getDepartment().getId();
		List<Department> toDepartments = departmentService.findList("department.id",dId);
		model.addAttribute("toDepartments", toDepartments);
	}

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model, HttpServletRequest request) {
		MsgNotice msgNotice = msgNoticeService.find(id);
		model.addAttribute(msgNotice);
		commonModel(model, request);
		return "/adoms/msgNotice/input";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(MsgNotice msgNotice) {
		if (msgNotice.isNew()) {
			msgNoticeService.save(msgNotice);
		} else {
			msgNoticeService.update(msgNotice);
		}

		return "redirect:/adoms/msgNotice/list";// 重定向
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id) {
		msgNoticeService.delete(id);
		return "redirect:/adoms/msgNotice/list";// 重定向
	}

	@RequestMapping(value = "/visible/{id}")
	public String visible(@PathVariable("id") Integer id) {
		msgNoticeService.visible(id);
		return "redirect:/adoms/msgNotice/list";// 重定向
	}

	@RequestMapping(value = "/unVisible/{id}")
	public String unVisible(@PathVariable("id") Integer id) {
		msgNoticeService.unVisible(id);
		return "redirect:/adoms/msgNotice/list";// 重定向
	}

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}

	private Users getCurrentUser(HttpServletRequest request) {
		Users user = (Users) request.getSession().getAttribute("USER");
		return user;
	}

}
